package com.example.demo.config;


import com.example.demo.bean.DeepSeekRequest;
import com.example.demo.service.DeepSeekService;
import com.example.demo.utils.PromptLoaderUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

@Component
@RequiredArgsConstructor
public class DeepSeekParallelClient {

    @Resource
    private final DeepSeekService deepSeekService;
    @Autowired     // 指定 Bean 名称
    private final Executor deepSeekPool;   // 注入线程池

    /**
     * 并行生成副标题 + 语音文案
     * @param title 城市标题
     * @return [0] 副标题  [1] 语音文案
     */
    public List<String> generateBoth(String title) throws Exception {



        String promptSub  = new PromptLoaderUtil("/prompt_sub.txt").getPrompt();
        String promptVoice= new PromptLoaderUtil("/prompt_voice.txt").getPrompt();

        System.out.println("sub:" + promptSub);
        System.out.println("voice" + promptVoice);

        CompletableFuture<String> subFuture =
                CompletableFuture.supplyAsync(() ->
                                deepSeekService.callDeepSeek("[" + title + "]",
                                        List.of(new DeepSeekRequest.Message("system", promptSub))),
                        deepSeekPool);

        CompletableFuture<String> voiceFuture =
                CompletableFuture.supplyAsync(() ->
                                deepSeekService.callDeepSeek("[" + title + "]",
                                        List.of(new DeepSeekRequest.Message("system", promptVoice))),
                        deepSeekPool);

        // 等待两个都完成（异常自动抛出）
        return CompletableFuture.allOf(subFuture, voiceFuture)
                .thenApply(v -> List.of(subFuture.join(), voiceFuture.join()))
                .get();        // 这里阻塞直到拿到结果
    }
}
